package com.hjx.controller;

import com.hjx.entity.SysDepartment;
import com.hjx.logging.LogAnnotation;
import com.hjx.response.AjaxResponse;
import com.hjx.response.ResponseType;
import com.hjx.service.SysDepartmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2020/6/11 - 14:17
 */
@RestController
@RequestMapping("dep")
public class SysDepartmentController {
    @Resource
    SysDepartmentService sysDepartmentService;
    private Logger log = LoggerFactory.getLogger(SysDepartmentController.class);

    @LogAnnotation
    @GetMapping("/findOne/{id}")
    public AjaxResponse findOne(@PathVariable String id) {
        SysDepartment department = sysDepartmentService.findOne(id);
        if (department != null) {
            return new AjaxResponse(ResponseType.QUERY_SUCCESS, department);
        }
        return new AjaxResponse(ResponseType.QUERY_FAILED);
    }

    @LogAnnotation
    @GetMapping("/findAll")
    public AjaxResponse findAll() {
        log.info("进入部门全查");
        List<SysDepartment> list = sysDepartmentService.findAll();
        log.info("查询到的信息为:" + list.toString());
        return new AjaxResponse(ResponseType.QUERY_SUCCESS, list);
    }

    @LogAnnotation
    @PostMapping("/findBy")
    public AjaxResponse findBy(@RequestBody SysDepartment department) {
        return new AjaxResponse(ResponseType.QUERY_SUCCESS, sysDepartmentService.findBy(department));
    }

    @LogAnnotation
    @PostMapping("/insert")
    public AjaxResponse insert(@RequestBody SysDepartment sysDepartment) {
        boolean result = sysDepartmentService.insert(sysDepartment);
        if (result) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        } else {
            return new AjaxResponse(ResponseType.OPERATION_FAILED);
        }
    }

    @LogAnnotation
    @PostMapping("/update")
    public AjaxResponse update(@RequestBody SysDepartment department) {
        boolean result = sysDepartmentService.update(department);
        if (result) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        } else {
            return new AjaxResponse(ResponseType.OPERATION_FAILED);
        }
    }

    @LogAnnotation
    @GetMapping("/delete/{id}")
    public AjaxResponse delete(@PathVariable String id) {
        boolean result = sysDepartmentService.delete(id);
        if (result) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        } else {
            return new AjaxResponse(ResponseType.OPERATION_FAILED);
        }
    }
}
